package com.ktg.mes.unit.apiCode.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.mes.unit.apiCode.mapper.ApiCodeMapper;
import com.ktg.mes.unit.apiCode.service.IApiCodeService;
import com.ktg.mes.unit.model.Model1ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ktg.mes.unit.apiCode.domain.ApiCode;


/**
 * api 接口配置Service业务层处理
 *
 * @author byteco
 * @date 2022-11-25
 */
@Service
public class ApiCode1ServiceImpl extends Model1ServiceImpl<ApiCodeMapper, ApiCode> implements IApiCodeService
{

    @Override
    public List<Map> selectMap(String selectSglString) {
        return getBaseMapper().selectMap(selectSglString);
    }

    /**
     * 查询api 接口配置
     *
     * @param entity api 接口配置
     * @param page 分页对象
     * @return api 接口配置
     */
    @Override
    public IPage<ApiCode> queryPageList(ApiCode entity, Page<ApiCode> page) {
        LambdaQueryWrapper<ApiCode> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(ApiCode::getCreateTime);
        IPage<ApiCode> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询api 接口配置
     *
     * @param entity api 接口配置
     * @return api 接口配置
     */
    @Override
    public List<ApiCode> queryList(ApiCode entity) {
        LambdaQueryWrapper<ApiCode> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(ApiCode::getCreateTime);
        return this.list(lqw);
    }

    public ApiCode getInfo(Long id)
    {
        QueryWrapper<ApiCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApiCode::getApiCode, id);
        return this.getOne(queryWrapper);
    }


    /**
     * 新增api 接口配置
     *
     * @param entity api 接口配置
     * @return 结果
     */
    @Override
    public void insert(ApiCode entity)
    {
        entity.setCreateTime(DateUtils.getNowDate());
        this.save(entity);
    }

    /**
     * 修改api 接口配置
     *
     * @param entity api 接口配置
     * @return 结果
     */
    @Override
    public boolean update(ApiCode entity)
    {
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除api 接口配置
     *
     * @param apiCodes 需要删除的api 接口配置主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] apiCodes)
    {

        return this.removeByIds(Arrays.asList(apiCodes));
    }

    /**
     * 删除api 接口配置信息
     *
     * @param apiCode api 接口配置主键
     * @return 结果
     */
    @Override
    public boolean delete(Long apiCode)
    {
        return this.removeById(apiCode);
    }

    private LambdaQueryWrapper<ApiCode> buildQueryWrapper(ApiCode entity) {
        LambdaQueryWrapper<ApiCode> lqw = Wrappers.lambdaQuery();
                    lqw.eq(entity.getApiCode() != null, ApiCode::getApiCode, entity.getApiCode());
                    lqw.eq(StringUtils.isNotBlank(entity.getTitle()), ApiCode::getTitle, entity.getTitle());
                    lqw.eq(StringUtils.isNotBlank(entity.getDes()), ApiCode::getDes, entity.getDes());
                    lqw.eq(StringUtils.isNotBlank(entity.getDsName()), ApiCode::getDsName, entity.getDsName());
                    lqw.eq(StringUtils.isNotBlank(entity.getSqlString()), ApiCode::getSqlString, entity.getSqlString());
                    lqw.eq(StringUtils.isNotBlank(entity.getParam()), ApiCode::getParam, entity.getApiCode());
                    lqw.eq(StringUtils.isNotBlank(entity.getResult()), ApiCode::getResult, entity.getResult());
        return lqw;
    }
}
